<script setup>
import { ref } from 'vue'
import Swiper from '@/components/Swiper/index.vue'
import { getSueTemplate, createSue } from '@/api/arrears'
import { ElMessage } from 'element-plus'

const props = defineProps({
  info: {
    type: Object,
  },
})
const emit = defineEmits(['success', 'close'])

const loading = ref(false)
const templateList = ref([])
const getTemplateList = async () => {
  loading.value = true
  try {
    const { data } = await getSueTemplate()
    templateList.value = data || []
  } catch (error) {
    console.error('获取模板列表失败:', error)
  }
  loading.value = false
}
getTemplateList();

const active = ref(0);
const onChange = (i) => {
  active.value = i;
}

const submitloading = ref(false);
const onSubmit = async () => {
  if (submitloading.value) return;
  const data = templateList.value[active.value].extra_format_data || [];
  if (data.some(item => item.value === null || item.value === undefined || item.value === '')) {
    ElMessage.warning('请将表单填写完整');
    return;
  }
  const form = data.reduce((acc, item) => {
    acc[item.key] = item.value;
    return acc;
  }, {});

  submitloading.value = true;
  try {
    const { data } = await createSue({
      arrears_data_id: props.info.arrears_data_id,
      template_id: templateList.value[active.value].id,
      extra_format_data: form
    });
    if (data) {
      ElMessage.success('操作成功');
      const link = document.createElement('a');
      link.href = data.url;
      link.download = ''; // You can customize the file name here
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      emit('success');
      onClose();
    }
  } catch (error) {
    console.error('提交失败:', error);
  }
  submitloading.value = false;
}
const onClose = () => {
  emit('close');
}
</script>

<template>
  <div>
    <Swiper :categories="templateList" @change="onChange" v-loading="loading" class="!h-[200px]">
      <template #default="{ cat }">
        <div class="p-4 rounded border border-zinc-300 h-full">
          <div class="text-center text-lg mb-4">{{ cat?.title }}</div>
          <template v-for="(item, index) in cat.extra_format_data" :key="index">
            <div class="flex items-center mb-4">
              <div class="w-32 text-right">{{ item.key }}</div>
              <div class="flex-1 ml-4">
                <el-input v-if="item.type === 'text'" v-model="item.value" :placeholder="'请输入' + item.key" />
                <el-input-number v-else-if="item.type === 'money'" v-model="item.value" :controls="false"
                  :placeholder="'请输入' + item.key" :precision="2" />
                <el-date-picker v-else-if="item.type === 'date' || item.type === 'datetime'" v-model="item.value"
                  :type="item.type" :placeholder="'请输入' + item.key"
                  :value-format="item.type === 'date' ? 'YYYY-MM-DD' : 'YYYY-MM-DD HH:mm:ss'" />
              </div>
            </div>
          </template>
        </div>
      </template>
    </Swiper>
    <div class="mt-4 text-right">
      <el-button type="primary" @click="onSubmit()" :loading="submitloading">确定</el-button>
      <el-button @click="onClose()">取消</el-button>
    </div>
  </div>
</template>